import axios from 'axios'

import { useCookies } from 'vue3-cookies'
import router from '@/router'
import { ElMessage } from 'element-plus'

const { cookies } = useCookies()

axios.defaults.timeout = 30000
axios.defaults.baseURL = 'http://127.0.0.1:8899'

// 请求拦截器
axios.interceptors.request.use(
  (config) => {
    // 每次发送请求之前判断是否存在token，如果存在，则统一在http请求的header都加上token，不用每次请求都手动添加了
    // 即使本地存在token，也有可能token是过期的，所以在响应拦截器中要对返回状态进行判断
    config.headers['Authorization'] = localStorage.getItem('token')
    if (config.method.toUpperCase() === 'POST') {
      config.headers['Content-Type'] = 'application/json;charset=utf-8'
    }
    return config
  },
  (error) => {
    return Promise.error(error)
  },
)

// 返回拦截器
axios.interceptors.response.use(
  (res) => {
    const back_data = res.data
    if (back_data.code !== 0) {
      ElMessage.error(back_data.msg)
    }

    if (back_data.code === -901) {
      router.push('/login')
    }

    return Promise.resolve(res)
  },
  (error) => {
    switch (error.response.status) {
      case 500:
        ElMessage.error('Oops,Server Error.')
        break
      case 404:
        ElMessage.error('Oops, Not Find!')
        break
      case 400:
        // ElMessage.error('Oops, 400!')
        break
      // 其他错误，直接抛出错误提示
      default:
        ElMessage.error(error.message)
    }
    return Promise.reject(error)
  },
)

function f_base_get(url, params) {
  return new Promise((resolve, reject) => {
    axios
      .get(url, { params })
      .then((res) => {
        resolve(res.data)
      })
      .catch((err) => {
        reject(err)
      })
  })
}

function f_base_post(url, params) {
  return new Promise((resolve, reject) => {
    axios
      .post(url, params)
      .then((res) => {
        resolve(res.data)
      })
      .catch((err) => {
        reject(err)
      })
  })
}

function f_base_put(url, params) {
  return new Promise((resolve, reject) => {
    axios
      .put(url, params)
      .then((res) => {
        resolve(res.data)
      })
      .catch((err) => {
        reject(err)
      })
  })
}

function f_base_delete(url, params) {
  return new Promise((resolve, reject) => {
    axios
      .delete(url, params)
      .then((res) => {
        resolve(res.data)
      })
      .catch((err) => {
        reject(err)
      })
  })
}

//  将Promise 的错误统一捕获
export async function Post(url, params) {
  let back = null
  try {
    back = await f_base_post(url, params)
  } catch (e) {
    console.log('async post', e)
  }
  return back
}

export async function Get(url, params) {
  let back = null
  try {
    back = await f_base_get(url, params)
  } catch (e) {
    console.log('async get', e)
  }
  return back
}

export async function Put(url, params) {
  let back = null
  try {
    back = await f_base_put(url, params)
  } catch (e) {
    console.log('async post', e)
  }
  return back
}

export async function Delete(url, params) {
  let back = null
  try {
    back = await f_base_delete(url, params)
  } catch (e) {
    console.log('async post', e)
  }
  return back
}
